#!/usr/bin/python

#-------------------------------------------------------------------------------------------------
# Name:        piece
# Purpose:     piece class to do various operations
#
# Author:      cackles
#
# Created:     06/25/2011
#-------------------------------------------------------------------------------------------------

import getopt
import sys
import os
from localtypes import Position, Velocity, Logger 
from pygame import sprite, image, mask, time
from genericpath import exists

#
# use filter() for calling a function multiple times over a list (returning elements for return=true)
#  filter(collision, listofobjects)
#

class piece(Logger, sprite.DirtySprite):
    
    def __init__(self):
        self.position = Position(0, 0, 0)
        self.velocity = Velocity(0, 0, 0)
        self.maxvelocity = Velocity(1,1,1)
        
        try:
            if exists(self.imagefile):
                pass
        except:
            # check to see if the image actually exists for this piece. 
            # if it doesn't exist, then this is probably a test bench so, assign one
            import pygame
            pygame.init()
            size = 5*64, 5*64 # width, height
            pygame.display.set_mode(size, pygame.DOUBLEBUF)
            self.imagefile = "images" + os.sep + "city.png"
            
        self.image = image.load(self.imagefile).convert_alpha()
        self.mask = mask.from_surface(self.image)
        self.rect  = self.image.get_rect()
        self.name = "piece" 
        self.offense = 1
        self.defense = 1
        self.health = 1
        self.moves = 1
        self.lastUpdate = time.get_ticks()
        sprite.DirtySprite.__init__(self) 
        return    
    
    def setMaxVelocity(self, maxvelocity):
        self.maxvelocity.x = maxvelocity.x
        self.maxvelocity.y = maxvelocity.y
        self.maxvelocity.z = maxvelocity.z
        
    def setVelocity(self, velocity):
        self.velocity.x = velocity.x
        self.velocity.y = velocity.y
        self.velocity.z = velocity.z

    def setPosition(self, position):
        self.position.x = position.x
        self.position.y = position.y
        self.position.z = position.z
        self.rect.move_ip(self.position.x*64, self.position.y*64) 
    
    def update(self, time, boundary_width=0, boundary_height=0):
        if self.rect.left < 0 or self.rect.right > boundary_width:
            self.velocity.x *= -1
        if self.rect.top < 0 or self.rect.bottom > boundary_height:
            self.velocity.y *= -1

        if (self.velocity.x > self.maxvelocity.x):
            self.velocity.x = self.maxvelocity.x
        if (self.velocity.x < -1*self.maxvelocity.x):
            self.velocity.x = -1*self.maxvelocity.x             
        if (self.velocity.y > self.maxvelocity.y):
            self.velocity.y = self.maxvelocity.y
        if (self.velocity.y < -1*self.maxvelocity.y):
            self.velocity.y = -1*self.maxvelocity.y
        if (self.velocity.z > self.maxvelocity.z):
            self.velocity.z = self.maxvelocity.z
        if (self.velocity.z < -1*self.maxvelocity.z):
            self.velocity.z = -1*self.maxvelocity.z
            
        self.position += (self.velocity * time)
        self.rect.move_ip((time * self.velocity.x, time * self.velocity.y))
        #self.rect.center = (round(time * self.velocity.x),round(time * self.velocity.y)) 
        #self.rect.left = self.position.x
        #self.rect.top = self.position.y

    def predictFuturePosition(self, predictionTime):
        pass

    # true or false
    def isMovable(self):
        if self.moves > 0 and self.health > 0:
            return True
        return False
        

    # assign number of movement spaces
    def assignMoves(self):
        pass

    # returns a list of possible moves for this object
    def queryMoves(self):
        pass

    # moves this object
    def move(self):
        pass

    # destroy this object
    def destroy(self):
        pass
    
    def __del__(self):
        pass

 
    def __output(self):    
        pass
    
                    
class tank(piece):
    def __init__(self):
        self.imagefile = "images" + os.sep + "player2.png"
        piece.__init__(self)
        self.mass = 0
        self.maxvelocity = Velocity(80,80,80)
        
    def __del__(self):
        piece.__del__(self)

class player(piece):
    def __init__(self):
        self.imagefile = "images" + os.sep + "player2.png"
        piece.__init__(self)
        self.mass = 0
        self.maxvelocity = Velocity(60,60,60)
        
    def __del__(self):
        piece.__del__(self)   

def usage():
    print "Usage: [-h] [-p <pieces>]"
    print " Required:"
    print "  -p,--pieces <pieces>               Number of pieces on the piece"
    print " Optional:"
    print "  -h,--help                          Print this usage information"

# main method
def main():
    pieces = 0

    try:
        # set up the arguments available to the user
        opts, args = getopt.getopt(sys.argv[1:], "p:h", ["pieces=", "help"])

    except getopt.GetoptError, err:
        print str(err)
        usage()
        sys.exit(1)

    # loop through the arguments passed by the user
    for o, a in opts:
        if o in ("-h", "--help"):
            usage()
            sys.exit()
        elif o in ("-p", "--pieces"):
            pieces = a
        else:
            assert False, "unhandled option"
            usage()

    if pieces == 0:
        usage()
        sys.exit()

    p = piece()
    t = tank()
    t.setPosition(Position(1,2,3))
    
    p.position += Position(0,2,4)
    p.position += Position(2,2,2)
    p.position -= Position(3,3,3)
    
    p.info("this is the info message")
    print "piece: %s" % (p.position)
    print "tank: %s" % (t.position)
    t.error("tank message")

    if p.position == t.position:
        print "p and t positions are equal"
    else:
        print "p and t are %f apart" % (p.position.dist(t.position))

    sys.exit()

if __name__ == '__main__':
    main()
